<?php
/**
 *
 *
 */
namespace Dclub\modules\Catalog;

class Controller_Default
    extends \Yz_Controller_Templated
    implements \Yz_Controller_Interface
{


    /**
     *
     */
    protected function doGET()
    {
        debug();

        $args = $this->router->getArguments();
        $argc = $args->count();

        switch ($argc) {
            case 0: // no cat selected, show the top levels (2)
                $tree = $this->loadTreeTop();
                $tplName = null; // use default template
                break;
            case 1: // assert a top cat (type) selected, show the subtree for the type
                $tree = $this->loadSubTreeOfType($args[0]);
                // TODO also include the list of wares marked for presence on this page
                $tplName = 'type';
                break;
            case 2: // this is when the wares list (by category) appears
            default:
                // do not load the whole tree, only the path and the category's siblings/children --
                // other cats appear on-demand via AJAX
                $tree = null;
                $treePath = $this->loadCatByPath($args);
                $list = $this->loadListByCat($treePath[-1]);
                $catChildren = $this->loadCatChildren($treePath[-1]);
                $tplName = 'list-by-cat';

        }
        $template = $this->selectTemplate($tplName);
        if ($tree) {
            $template->tree = $tree;
        } else {
            $template->treePath = $treePath;
            $template->list = $list;
            $template->catChildren = $catChildren;


        }

        return $this->outputTemplate($template);
    }

    /**
     *  fetch the two topmost levels of the category tree
     *  @return Yz_Tree_Interface
     */
    protected function loadTreeTop()
    {
        $depth = 2;
        $model = $this->module->selectModel('Tree');
        $treeTop = $model->fetchTreeToDepth($depth);
        return $treeTop;
    }

    /**
     *  fetch the branch of the category tree which starts at a root node specified by name
     *  @param string $typeName root node name
     *  @return Yz_Tree_Interface
     *
     */
    protected function loadSubTreeOfType($typeName)
    {
        $depth = 2;
        $model = $this->module->selectModel('Tree');
        $typeBranch = $model->fetchTypeBranchByName($typeName);
        return \Dclub_Tree::fromTree($typeBranch);
    }

    /**
     *  identify the category path by the specified path component names
     *  @return array{Item_Category} path
     */
    protected function loadCatByPath(\Yz_Array $args)
    {
        debug();
        $model = $this->module->selectModel('TreePath');
        $typePath = $model->fetchPathByNames($args);
        return $typePath;
    }

    /**
     *  load list of wares for the specified category
     *  @param Item_Category
     *  @return array{Item_Default}
     */
    protected function loadListByCat($cat)
    {
        debug();
        $model = $this->module->selectModel('List');
        $list = $model->fetchListByCat($cat->id);
        return $list;
    }

    /**
     *  load list of wares for the specified category
     *  @param Item_Category
     *  @return array{Item_Category}
     */
    protected function loadCatChildren($cat)
    {
        debug();
        $model = $this->module->selectModel('CategoryList');
        $list = $model->fetchCatChildren($cat->id);
        return $list;
    }

}
